/*!
 * New Launch.
 * Date: 2015/12/29.
 * Copyright© 2014-2015 Vamaker All Rights Reserved.
 */
require([
    'jquery',
		'pageInit',
		'formValidator',
    'formAjax',
    'notification',
    'select2'
	], function ($, vamakerPage, formValidator, formAjax, VamOpMsg, select2) {
  $(function () {
    $('#advertiser_resource').select2();
    $('#govern_users').select2();
    $('#filter_media').select2();
    $('#filter-traffic').select2();
    $('#filter-channel').select2();
  });

  $(function () {
    var $dataFrm = $('#data-frm'),
        $submitBtn = $dataFrm.find('button[type="submit"]'),
        dataFieldRule, options, dataValidator;
    // console.log($dataFrm);
    dataFieldRule = {
      user_email:{
        rule:{
            required:{
                message: VAM.j('The login email can not be empty')
            },
            email:{
                message: VAM.j('Please enter the correct email')
            }
        },
        messageContainer:'user_email_err'
      },
      user_name:{
        rule:{
            required:{
                message: VAM.j('Username cannot be empty')
            },
            username:{
                message: VAM.j('Username only be letter、number、line-through、underline、And begin with letter')
            },
            maxlength:{
                param: 32,
                message: VAM.j('Mailbox for a maximum of 200 characters')
            }
        },
        messageContainer:'user_name_err'
      },
      user_pwd:{
        rule:{
            required:{
              param:'password',
              message: VAM.j('Password cannot be empty')
            },
            password:{
                message: VAM.j('Password must be character, number')
            },
            minlength:{
                param:6,
                message: VAM.j('Minimum password length of 6 characters')
            },
            maxlength:{
                param:20,
                message: VAM.j('Password length up to 20 characters')
            },
            password1:{
                param:$('#confirm_pwd'),
                message: VAM.j('Please enter password')
            }
        },
        messageContainer:'user_pwd_err',
        eventName:'change'
      },
      confirm_pwd:{
        rule:{
            required:{
                param:'password',
                message: VAM.j('Please enter confirm password')
            },
            password2:{
                param:$('#user_pwd'),
                message: VAM.j('The password you entered is not consistent with the first input')
            }
        },
        messageContainer:'confirm_pwd_err'
      }
      // ,
      // advertiser_resource: {
      //   rule: {
      //     required: {
      //       message: '广告主资源不能为空'
      //     }
      //   },
      //   messageContainer: 'advertiser_resource_err'
      // },
      // govern_users: {
      //   rule: {
      //     required: {
      //       message: '所管辖用户不能为空'
      //     }
      //   },
      //   messageContainer: 'govern_users_err'
      // }
    };
    options = {
      submitOnSucceed: false,
      onvalidate: function(evt) {
        if (evt.resultList.length <= 0) {
            $dataFrm.ajaxSubmit({
                beforeSubmit: function() {
                  $submitBtn.button('loading');
                },
                success: function (data) {
                  $submitBtn.button('reset');
                  if (data.status === 'success') {
                    VamOpMsg.messager(data);
                    window.location.href = 'user-list';
                  } else {
                    VamOpMsg.messager(data);
                  }
                }
            });
        };
      }
    };
    formValidator.validate($dataFrm, dataFieldRule, options);
    dataValidator = formValidator.dataValidator;
    dataValidator.addRule('password1', function(val, opt){
        opt.trigger('change');
        return true;
      });
    dataValidator.addRule('password2', function(val, opt){
      return val===opt.val();
    });
    dataValidator.addRule('effectiveNumber',function(val, opt){
      return !!(val && /^\d+(\.\d{1,2})?$/.test(val));
    });
    dataValidator.addRule('required', function(val, opt){
      if(opt==='password'){
          return !!(val&&!/^(?:\s\w|\u3000)+$/.test(val));
      }else{
          return !!(val&&!/^(?:\s|\u3000)+$/.test(val));
      }
    });
  });
});
